package console;

import java.awt.Color;
import java.awt.Graphics;

public class CmdLine
{
	private Color bgColor, borderColor, txtColor, recordColor, recordBgColor;
	private StringBuffer cmd;
	private int x, y, width, height;
	private final int ARC_SIZE = 2;
	private String[] record = {"","","","",""};
	private int pointer = 0;
	
	private boolean isVisible;
	
	public CmdLine(int x, int y, int width, int height)
	{

		cmd = new StringBuffer();
		bgColor = new Color(157, 157, 161, 160);
		txtColor = new Color(236, 170, 64, 255);
		borderColor = new Color(75, 96, 121, 255);
		recordBgColor = new Color(45, 32, 7, 160);
		recordColor = new Color(243, 254, 242, 255);
		
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		isVisible = false;
	}
	
	public boolean isVisible()
	{
		return isVisible;
	}
	
	public void setVisible(boolean v)
	{
		isVisible = v;
	}
	
	public void draw(Graphics g)
	{
		if (this.isVisible)
		{
			g.setColor(borderColor);
			g.drawRoundRect(x, y, width, height + (height<<2), ARC_SIZE, ARC_SIZE);
			g.drawRoundRect(x, y + (height<<2), width, height, ARC_SIZE, ARC_SIZE);
			g.setColor(recordBgColor);
			g.fillRoundRect(x + 1, y + 1, width - 1, (height<<2) - 1, ARC_SIZE, ARC_SIZE);
			g.setColor(bgColor);
			g.fillRoundRect(x+1, y + (height<<2) + 1, width-1, height-1, ARC_SIZE, ARC_SIZE);
			g.setColor(txtColor);
			g.drawString(cmd.toString(), x+10, y + (height<<2) + 15);
			g.setColor(recordColor);
			for (int i = 0; i < record.length; i++)
			{
				g.drawString(record[(pointer + i)%record.length], x + 10, y + (height << 2) - 5 - (15*(record.length - i - 1)));
			}
		}
		else
		{
			g.setColor(borderColor);
			g.drawLine(x, y, width, y);
			g.drawLine(x, y+1, width, y+1);
		}
	}
	
	public void record(String command)
	{
		if (command.length() > 0 && !command.matches("[ \\t]"))
		{
			record[pointer] = command;
			pointer = (pointer+1)%record.length;
		}
	}
	
	public void addCmd(char c)
	{
		cmd.append(c);
	}
	
	public void clear()
	{
		if (cmd.length() > 0)
			//cmd.delete(0, cmd.length() - 1);
			cmd.setLength(0);
	}
	
	public void delPre()
	{
		if (cmd.length() > 0)
			//cmd.deleteCharAt(cmd.length() - 1);
			cmd.setLength(cmd.length() - 1);
	}
	
	public String getCommand()
	{
		return cmd.toString();
	}
}
